package com.ykw.parking.controller;

import com.ykw.parking.mapper.OrderMapper;
import com.ykw.parking.mapper.UserMapper;
import com.ykw.parking.pojo1.RealUser;
import com.ykw.parking.pojo1.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class RealNameController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;
    @RequestMapping("/realName")
    public String realName(Model model,HttpSession session){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        RealUser realUser = orderMapper.queryUser(user.getId());
        if (realUser!=null){
            model.addAttribute("realMsg",1);
        }else{
            model.addAttribute("realMsg",0);
        }
        return "realName";
    }

    @RequestMapping(value = "/getNameAndNumber",method = RequestMethod.POST)
    @ResponseBody
    public String getNameAndNumber(String rname, String rnumber, HttpSession session){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        RealUser realUser = orderMapper.queryUser(user.getId());
        if (realUser!=null){
            return "您已经实名";
        }
        userMapper.insertRealUser(user.getId(),user.getName(),user.getPhone_number(),user.getEmail(),rname,rnumber);

        return "实名成功";
    }
}
